C++ User-defined Function Types

03-11-17 Course- CPP

For better understanding of arguments and return in functions, user-defined functions can be categorised as:

  • Function with no argument and no return value
  • Function with no argument but return value
  • Function with argument but no return value
  • Function with argument and return value

Consider a situation in which you have to check prime number. This problem is solved below by making user-defined function in 4 different ways as mentioned above.

Function with no argument and no return value


# include <iostream>
using namespace std;

void prime();

int main() {
    prime();      // No argument is passed to prime().
    return 0;
}


// Return type of function is void because value is not returned.
void prime() {

    int num, i, flag = 0;
    printf("Enter a positive integer enter to check: ");
    cin>>num;
    for(i = 2; i <= num/2; ++i){
        if(num%i == 0) {
            flag=1; 
            break;
        }
    }
    if (flag == 1) {
        cout<<num<<" is not a prime number.";
    }
    else {
        cout<<num<<" is a prime number.";
    }
}

Function with no arguments but return value


#include <iostream>
using namespace std;

int prime();

int main() {
    int num, i, flag = 0;
    num = prime();     /* No argument is passed to prime() */
    for (i = 2; i <= num/2; ++i) {
        if (num%i == 0) {
            flag = 1;
            break;
        }
    }
    if (flag == 1) {
        cout<<num<<" is not a prime number.";
    }
    else {
        cout<<num<<" is a prime number.";
    }
    return 0;
}

// Return type of function is int
int prime() {
    int n;
    printf("Enter a positive integer to check: ");
    cin>>n;
    return n;
}

Function with argument but no return value


#include <iostream>
using namespace std;

void prime(int n);

int main() {
    int num;
    cout<<"Enter a positive integer to check: ";
    cin>>num;
    
    prime(num);  // Argument num is passed to function. 
    return 0;
}

// There is no return value to calling function. Hence, return type of function is void. */
void prime(int n) {
    int i, flag = 0;
    for (i = 2; i <= n/2; ++i) {
        if (n%i == 0) {
            flag = 1;
            break;
        }
    }
    if (flag == 1) {
        cout<<n<<" is not a prime number.";
    }
    else {
        cout<<n<<" is a prime number.";
    }
}

Function with arguments and return value.


#include <iostream>
using namespace std;

int prime(int n);

int main() {
    int num, flag = 0;
    cout<<"Enter positive enter to check: ";
    cin>>num;
    flag = prime(num); /* Argument num is passed to check() function. */
    if(flag == 1)
        cout<<num<<" is not a prime number.";
    else
        cout<<num<<" is a prime number.";
    return 0;
}

/* This function returns integer value.  */
int prime(int n){
    int i;
    for(i = 2; i <= n/2; ++i){
        if(n%i == 0)
            return 1;
    }
    return 0;
}

All four programs above gives the same output and all are technically correct program. There is no hardest and fastest rule on which method should be chosen. The particular method is chosen depending upon the situation and how a programmer want to solve that problem.